﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
namespace Hotel.Operations
{
    public partial class Payments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPaymentMode();

                StartDateTxtBox.Text = DateTime.Today.AddDays(-7).ToString("MM/dd/yyyy");
                EndDateTxtBox.Text = DateTime.Today.ToString("MM/dd/yyyy");
                LoadPayments();
              
            }


        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            LoadPayments();
        }

        private void LoadPaymentMode()
        {
            DataSet ds = HotelManger.GetInstance().GetAllPaymentMode(0);

            if (ds.Tables[0].Rows.Count > 0)
            {
                PaymentModeDropDownList.DataSource = ds;
                PaymentModeDropDownList.DataTextField = "PaymentMode";
                PaymentModeDropDownList.DataValueField = "PaymentModeId";

                PaymentModeDropDownList.DataBind();
                PaymentModeDropDownList.Items.Insert(0, new ListItem("", ""));
            }
        }

        private void LoadPayments()
        {
            int paymodeid = (PaymentModeDropDownList.SelectedValue == "") ? 0 : Convert.ToInt32(PaymentModeDropDownList.SelectedValue);
            string paymentcleared = (PaymentClearedDropDownList.SelectedValue == "") ? "" : PaymentClearedDropDownList.SelectedValue;
            DataSet ds = HotelManger.GetInstance().GetPayments(paymodeid, paymentcleared, StartDateTxtBox.Text, EndDateTxtBox.Text);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gvPayments.DataSource = ds;
                    gvPayments.DataBind();
                }
                else
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("PaymentId");
                    dt.Columns.Add("PaymentDescription");
                    dt.Columns.Add("PaymentDate");
                    dt.Columns.Add("Surcharge");
                    dt.Columns.Add("Amount");
                    dt.Columns.Add("ClearanceFlag");
                    dt.Columns.Add("Remarks");
                    dt.Columns.Add("Edit");
                    dt.Columns.Add("Delete");
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    gvPayments.DataSource = dt;
                    gvPayments.DataBind();
                    gvPayments.Rows[0].Visible = false;
                }
            }
        }
        protected void gvPaments_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvPayments.EditIndex = e.NewEditIndex;
            LoadPayments();
        }


        protected void gvPayments_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            HT.Data.Objects.Payments payments = new HT.Data.Objects.Payments();
            payments.PaymentId = Convert.ToInt32(gvPayments.DataKeys[e.RowIndex].Values[0].ToString());
            payments.PaymentModeId = 1;
            payments.PaymentDescription = ((TextBox)gvPayments.Rows[e.RowIndex].FindControl("txtdescription")).Text;
            payments.PaymentDate = Convert.ToDateTime(((TextBox)gvPayments.Rows[e.RowIndex].FindControl("txtpaymentDate")).Text);
            payments.Amount = Convert.ToDouble(((TextBox)gvPayments.Rows[e.RowIndex].FindControl("txtAmount")).Text);
            payments.Surcharge = Convert.ToDouble(((TextBox)gvPayments.Rows[e.RowIndex].FindControl("txtSurcharge")).Text);
            payments.ClearanceFlag = Convert.ToBoolean(Convert.ToInt32(((DropDownList)gvPayments.Rows[e.RowIndex].FindControl("PaymentClearedDropDownList")).SelectedValue));
            payments.Remarks = ((TextBox)gvPayments.Rows[e.RowIndex].FindControl("txtRemarks")).Text;

            HotelManger.GetInstance().UpdatePayments(payments);
            gvPayments.EditIndex = -1;
            LoadPayments();
        }

        protected void gvpayment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && gvPayments.EditIndex == e.Row.RowIndex)
            {
                DropDownList ddlType = (DropDownList)e.Row.FindControl("PaymentClearedDropDownList");
                //ddlType.DataSource = HotelManger.GetInstance().GetAllRoomType(1, 0);
                //ddlType.DataBind();
                ddlType.Items.FindByText((e.Row.FindControl("lblvalue") as Label).Text).Selected = true;
            }
        }

        protected void gvpaymentl_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            gvPayments.EditIndex = -1;
            LoadPayments();
        }
    }
}